<?php

/**
 * 用户操作接口 包括用户的注册、登录、 修改密码、注销登录
 * @author:zhulin
 * @since:2012 4.18
 */
class UserController extends BaseController {

    /**
     *  用户注册接口
     * return array(
     * <pre>
     *          UName     string      用户名称
     *          UId       int         用户唯一ID
     *          Passwd    string      用户注册密码
     *          BirthDay  datetime    用户出生日期
     *          Phone     int         用户注册手机号
     *          EmaiL    string       用户注册邮箱
     *         
     * </pre>
     * )
     */
    public function register() {
        //用户注册相关参数
        $name = $this->getParamString('Uname');
        $password = $this->getParamString('Password');
        $brithday = $this->getParamString('BirthDay');
        $sex = (int) $this->getParamInt('Sex', false);
        $phone = (string) $this->getParamString('Phone', false);
        $phone = empty($phone) ? '' : substr($phone, -11);
        $email = $this->getParamString('Email');

        //注册用户，返回用户详细信息
        $userInfo = User_Info::userRegister($name, $password, $brithday, $sex, $phone, $email);
        //发送短信到用户手机
       if (!empty( $phone )) {
            Helper_Smsapi::sendSms( $phone, '欢迎来到尚源，您的密码为：' . $password);
      }
      else{
          echo "发送失败";
      }
       $_SESSION['UId'] = $userInfo['UId'];
       $_SESSION['Uname'] = $userInfo['UName'];

       //$default = Product_Detail::getDefault();
       //Helper_Url::redirect('index.php');
    }

    /**
     * 用户登录接口
     * @param string Uname     用户名
     * @param string Password  密码
     * @return array
     * <pre>
     *  array(
     *      UId:int
     *      UName:string
     *      Password:string
     *  )
     * </pre>
     */
    public function login() {
        //登录验证
        $Uname = $this->getParamString('Uname');
        $Password = $this->getParamString('Password');
        //加密密码   
        $key = Common::getConfig('passEncrypt');
        $passwdHash = Helper_String::password_hash($Password, $key);
        $userInfo = User_Info::getUserInfo($Uname, $passwdHash);
        if (empty($userInfo)) {
            throw new AppException(3001);
        }
        $_SESSION['UId'] = $userInfo['UId'];
        $_SESSION['Uname'] = $userInfo['UName'];

        //记录用户登录之前的操作 
        if (isset($_COOKIE['userInquire'])) {
            //根据cookie 推送产品
            $result    = unserialize($_COOKIE['userInquire']);
            $recommend = Product_Detail::productCoommend($result);
 
            //判断用户登录前是否由之q前是否有session的值，写入mc db
            if(isset ($_SESSION['userInquire'] ) ){
               $result = unserialize($_SESSION['userInquire'] );
               //print_r( $result  );
               $uid = $_SESSION['UId'];    
                UserProduct_Inquire::getInstance()->cookieSaveCacheMysql( $result );

            }
            else{
                
                echo "wu";
            }
            
        } 
        //Helper_Url::redirect('index.php');
    }

}